﻿Imports System.Data.SqlClient
Public Class UCBackup
    Dim con, con1 As SqlConnection
    Dim cmd As SqlCommand
    Dim dread As SqlDataReader

    Private Sub UCBackup_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        server(".")
        server(".\sqlexpress")
    End Sub
    Sub server(ByVal str As String)
        con = New SqlConnection("Data Source=" & str & ";Database=Master;integrated security=SSPI;")
        con.Open()
        cmd = New SqlCommand("select *  from sysservers  where srvproduct='SQL Server'", con)
        dread = cmd.ExecuteReader
        While dread.Read
            cmbServer.Items.Add(dread(2))
        End While
        dread.Close()
    End Sub
    Sub connection()
        con = New SqlConnection("Data Source=" & Trim(cmbServer.Text) & ";Database=Master;integrated security=SSPI;")
        con.Open()
        cmbDatabase.Items.Clear()
        cmd = New SqlCommand("select * from sysdatabases", con)
        dread = cmd.ExecuteReader
        While dread.Read
            cmbDatabase.Items.Add(dread(0))
        End While
        dread.Close()
    End Sub

    Private Sub cmbserver_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbServer.SelectedIndexChanged
        connection()
    End Sub
    Sub query(ByVal que As String)
        On Error Resume Next
        cmd = New SqlCommand(que, con)
        cmd.ExecuteNonQuery()
    End Sub
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If ProgressBar1.Value = 100 Then
            Timer1.Enabled = False
            ProgressBar1.Visible = False
            MsgBox("Hoàn tất")
        Else
            ProgressBar1.Value = ProgressBar1.Value + 5
        End If
    End Sub
    Sub blank(ByVal str As String)
        If cmbServer.Text = "" Or cmbDatabase.Text = "" Then
            MessageBox.Show("Chọn Server & Database", "Thông báo")
            Exit Sub
        Else
            If str = "saoluu" Then
                SaveFileDialog1.FileName = cmbDatabase.Text
                SaveFileDialog1.ShowDialog()
                Timer1.Enabled = True
                ProgressBar1.Visible = True
                Dim s As String
                s = SaveFileDialog1.FileName
                query("backup database " & cmbDatabase.Text & " to disk='" & s & "'")
            ElseIf str = "phuchoi" Then
                OpenFileDialog1.ShowDialog()
                Timer1.Enabled = True
                ProgressBar1.Visible = True
                query("restore database " & cmbDatabase.Text & " from disk='" & OpenFileDialog1.FileName & "'")
            End If
        End If
    End Sub

    Private Sub btnSaoLuu_Click(sender As System.Object, e As System.EventArgs) Handles btnSaoLuu.Click
        blank("saoluu")
    End Sub

    Private Sub btnPhucHoi_Click(sender As System.Object, e As System.EventArgs) Handles btnPhucHoi.Click
        blank("phuchoi")
    End Sub
End Class
